{extend name="base"/}
{block name="resources"}
<style>
	.form-wrap{margin-top: 0;}
</style>
{/block}
{block name="main"}
<div class="layui-form form-wrap">
	<div class="layui-form-item">
		<label class="layui-form-label"><span class="required">*</span>品牌名称：</label>
		<div class="layui-input-inline len-long">
			<input name="brand_name" type="text" lay-verify="required" class="layui-input ">
		</div>
	</div>

	
	<div class="layui-form-item">
		<label class="layui-form-label">图片：</label>
		<div class="layui-input-block img-upload">
			<div class="upload-img-block">
				<div class="upload-img-box">
					<div class="upload-default" id="imgUpload">
						<div class="upload">
							<i class="iconfont iconshangchuan"></i>
							<p>点击上传</p>
						</div>
					</div>
					<div class="operation">
						<div>
							<i title="图片预览" class="iconfont iconreview js-preview" style="margin-right: 20px;"></i>
							<i title="删除图片" class="layui-icon layui-icon-delete js-delete"></i>
						</div>
						<div class="replace_img js-replace">点击替换</div>
					</div>
					<input type="hidden" name="image_url" value=""/>
				</div>
			</div>
		</div>
		<div class="word-aux">
			<p>建议图片尺寸：200px * 100px。</p>
			<p>图片格式：jpg、png、jpeg。</p>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">排序：</label>
		<div class="layui-input-inline">
			<input type="number" name="sort" class="layui-input" placeholder="0" autocomplete="off">
		</div>
	</div>
	
	<div class="form-row">
		<button class="layui-btn" lay-submit lay-filter="save">保存</button>
		<button type="reset" class="layui-btn layui-btn-primary" onclick="back()">返回</button>
	</div>
</div>
{/block}
{block name="script"}
<script>
	layui.use(['form'], function() {
		var form = layui.form,
			repeat_flag = false;//防重复标识
		form.render();
		
		/**
		 * 表单验证
		 */
		form.verify({
			num: function (value) {
				if (value == '') {
					return;
				}
				if (value%1 != 0) {
					return '排序数值必须为整数';
				}
				if (value < 0) {
					return '排序数值必须为大于0';
				}
			}
		});

		// 普通图片上传
		var logo_upload = new Upload({
			elem: '#imgUpload',
			url: ns.url('giftcard://shop/media/upload')
		});
		form.on('submit(save)', function (data) {
			
			if (repeat_flag) return false;
			repeat_flag = true;
			if(!data.field.image_url) logo_upload.delete();
			$.ajax({
				url: '{:addon_url("shop/goodsbrand/addBrand")}',
				data: data.field,
				dataType: 'json',
				type: 'post',
				success: function (res) {
					repeat_flag = false;
					
					if (res.code == 0) {
						layer.confirm('添加成功', {
							title:'操作提示',
							btn: ['返回列表', '继续添加'],
							yes: function(){
								location.href = ns.url("shop/goodsbrand/lists")
							},
							btn2: function() {
								location.href = ns.url("shop/goodsbrand/addBrand")
							}
						})
					}else{
						layer.msg(res.message);
					}
				}
			});
		});
	});
	
	function back(){
		location.href = ns.url("shop/goodsbrand/lists")
	}
</script>
{/block}